﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Configuration;
using System.Reflection;
using log4net;
using Microsoft.ECSS.Common.CommonServiceContract;

namespace Koonsoft.Tool.SendMessage
{
    /// <summary>
    /// 发送手机短信
    /// </summary>
    public class SendShortMessage
    {
        private readonly static ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public static void SendToSingleTarget(string moduleCode, string message)
        {
            SendShortMessageToSingleTarget(ConfigurationManager.AppSettings["BOSSWCFServiceUrl"], moduleCode, message, ConfigurationManager.AppSettings["AdminMobiles"]);
        }

        private static void SendShortMessageToSingleTarget(string wcfUrl, string moduleCode, string message, string mobiles)
        {
            // 检查配置文件节点是否齐全
            CheckConfig();

            if (!string.IsNullOrEmpty(mobiles))
            {
                foreach (string mobile in mobiles.Split(','))
                {
                    if (!string.IsNullOrEmpty(mobile))
                    {
                        SMSHelper smshelper = new SMSHelper();

                        smshelper.SendShortMessageWithoutTemplet("代维巡检", mobile, message, ConfigurationManager.AppSettings["IP"], DateTime.Now);

                        //client.SendSM(moduleCode, mobile, message, ConfigurationManager.AppSettings["IP"]);
                    }
                }
            }
        }

        private static void CheckConfig()
        {
            if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["BOSSWCFServiceUrl"]))
            {
                log.Warn("配置文件缺少BOSSWCFServiceUrl节点", new Exception("请检查Config文件AppSettings项是否存在BOSSWCFServiceUrl节点，并且该节点值不为空"));
            }

            if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["SMSWCFServiceUrl"]))
            {
                log.Warn("配置文件缺少SMSWCFServiceUrl节点", new Exception("请检查Config文件AppSettings项是否存在SMSWCFServiceUrl节点，并且该节点值不为空"));
            }

            if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["AdminMobiles"]))
            {
                log.Warn("配置文件缺少AdminMobiles节点", new Exception("请检查Config文件AppSettings项是否存在AdminMobiles节点，并且该节点值不为空"));
            }

            if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["IP"]))
            {
                log.Warn("配置文件缺少IP节点", new Exception("请检查Config文件AppSettings项是否存在IP节点，并且该节点值不为空"));
            }
        }

    }
}
